{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Solution(object):\n",
    "    def findAnagrams(self, s, p):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :type p: str\n",
    "        :rtype: List[int]\n",
    "        \"\"\"\n",
    "        anagrams = []\n",
    "\n",
    "        len_s = len(s)\n",
    "        len_p = len(p)\n",
    "        \n",
    "        if len_s < len_p:\n",
    "            return anagrams\n",
    "        \n",
    "        std = {}\n",
    "        curr = {}\n",
    "\n",
    "        for i in range(len_p):\n",
    "            if p[i] in std:\n",
    "                std[p[i]] += 1\n",
    "            else:\n",
    "                std[p[i]] = 1\n",
    "        \n",
    "            if s[i] in curr:\n",
    "                curr[s[i]] += 1\n",
    "            else:\n",
    "                curr[s[i]] = 1\n",
    "            \n",
    "        if curr == std:\n",
    "            anagrams.append(0)\n",
    "        \n",
    "        end = len_p\n",
    "        while end < len_s:\n",
    "            if curr[s[end-len_p]] == 1:\n",
    "                del curr[s[end-len_p]]\n",
    "            else:\n",
    "                curr[s[end-len_p]] -= 1                \n",
    "                \n",
    "            if s[end] in curr:\n",
    "                curr[s[end]] += 1\n",
    "            else:\n",
    "                curr[s[end]] = 1\n",
    "\n",
    "            if std == curr:\n",
    "                anagrams.append(end-len_p+1)\n",
    "            \n",
    "            end += 1\n",
    "\n",
    "        return anagrams"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "s = Solution()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.findAnagrams(\"\",\"a\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
